package main

import "fmt"

func sum(a, b int) int {
	return a + b
}

func sub(a int, b int) int {
	return a - b
}

func moreRet(a, b, c int) (int, int, int) {
	return a, b, c
}

func sumMore(nums ...int) {
	fmt.Print(nums, " ")
	total := 0
	for _, num := range nums {
		total += num
	}
	fmt.Println("total:", total)
}

func intSeq() func() int {
	i := 0 //隐藏i变量
	return func() int {
		i++
		return i
	}
}

func fact(n int) int {
	if n == 0 {
		return 1
	}
	return n * fact(n-1)
}

func zeroval(ival int) {
	ival = 0
}

func zeroptr(iptr *int) {
	*iptr = 0
}

func main() {
	t := sum(4, 5)
	fmt.Println("sum:", t)

	t = sub(10, 3)
	fmt.Println("sub:", t)

	a, b, c := moreRet(1, 2, 3)
	fmt.Println(a, b, c)

	_, _, d := moreRet(2, 3, 4)
	fmt.Println(d)

	sumMore(4, 5, 6)

	arr1 := [...]string{"1", "2"}
	fmt.Println(arr1)

	nextInt := intSeq()
	fmt.Println(nextInt())
	fmt.Println(nextInt())
	fmt.Println(nextInt())

	fmt.Println(fact(7))

	var fib func(n int) int
	fib = func(n int) int {
		if n < 2 {
			return n
		}
		return fib(n-1) + fib(n-2)
	}
	fmt.Println("fib:", fib(7))

	i := 1
	fmt.Println("initial:", i)

	zeroval(i)
	fmt.Println("zeroval:", i)

	zeroptr(&i)
	fmt.Println("zeroptr:", i)

	fmt.Println("pointer:", &i)
}
